package com.competition.commgr.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.competition.commgr.domain.Competitions;
import com.competition.commgr.domain.vo.CompetitionItemVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.competition.common.annotation.Log;
import com.competition.common.core.controller.BaseController;
import com.competition.common.core.domain.AjaxResult;
import com.competition.common.enums.BusinessType;
import com.competition.commgr.domain.Competitionitem;
import com.competition.commgr.service.ICompetitionitemService;
import com.competition.common.utils.poi.ExcelUtil;
import com.competition.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 赛项管理Controller
 * 
 * @author cui
 * @date 2025-01-27
 */
@RestController
@RequestMapping("/commgr/competitionitem")
public class CompetitionitemController extends BaseController
{
    @Autowired
    private ICompetitionitemService competitionitemService;

    /**
     * 查询赛项管理列表
     */
    @PreAuthorize("@ss.hasPermi('commgr:competitionitem:list')")
    @GetMapping("/list")
    public TableDataInfo list(Competitionitem competitionitem)
    {
        startPage();
        List<CompetitionItemVo> list = competitionitemService.selectCompetitionitemVoList(competitionitem);
        return getDataTable(list);
    }

    /**
     * 导出赛项管理列表
     */
    @PreAuthorize("@ss.hasPermi('commgr:competitionitem:export')")
    @Log(title = "赛项管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Competitionitem competitionitem)
    {
        List<Competitionitem> list = competitionitemService.selectCompetitionitemList(competitionitem);
        ExcelUtil<Competitionitem> util = new ExcelUtil<Competitionitem>(Competitionitem.class);
        util.exportExcel(response, list, "赛项管理数据");
    }

    /**
     * 获取赛项管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('commgr:competitionitem:query')")
    @GetMapping(value = "/{comItemId}")
    public AjaxResult getInfo(@PathVariable("comItemId") Long comItemId)
    {
        return success(competitionitemService.selectCompetitionitemByComItemId(comItemId));
    }

    /**
     * 新增赛项管理
     */
    @PreAuthorize("@ss.hasPermi('commgr:competitionitem:add')")
    @Log(title = "赛项管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Competitionitem competitionitem)
    {
        return toAjax(competitionitemService.insertCompetitionitem(competitionitem));
    }

    /**
     * 修改赛项管理
     */
    @PreAuthorize("@ss.hasPermi('commgr:competitionitem:edit')")
    @Log(title = "赛项管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Competitionitem competitionitem)
    {
        return toAjax(competitionitemService.updateCompetitionitem(competitionitem));
    }

    /**
     * 删除赛项管理
     */
    @PreAuthorize("@ss.hasPermi('commgr:competitionitem:remove')")
    @Log(title = "赛项管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{comItemIds}")
    public AjaxResult remove(@PathVariable Long[] comItemIds)
    {
        return toAjax(competitionitemService.deleteCompetitionitemByComItemIds(comItemIds));
    }

    @PreAuthorize("@ss.hasPermi('commgr:competitionitem:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<CompetitionItemVo> util = new ExcelUtil<CompetitionItemVo>(CompetitionItemVo.class);
        List<CompetitionItemVo> list = util.importExcel(file.getInputStream());
        System.out.println(list);
        String operName = getUsername();
        String message = competitionitemService.importCompetitionitem(list, updateSupport, operName);
        return success(message);
    }


    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<CompetitionItemVo> util = new ExcelUtil<CompetitionItemVo>(CompetitionItemVo.class);
        util.importTemplateExcel(response, "赛事类别数据");
    }
}
